﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class InternalTeamMessages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.InternalTeamMessages_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            OnlineUser ou = (OnlineUser)Session["User"];
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            if (!Page.IsPostBack)
            {
                this.SqlDataSource1.SelectParameters.Add("UserId", ou.UserId);
                this.OldMessagesDataSource.SelectParameters.Add("UserId", ou.UserId);
                //this.Title = Resources.Resource.InternalTeamMessages_PageTitle;
            }
            //Response.Write(NewMailsCounter(UsrId));
            int NumOfUnreadMessages = (NewMessagesCounter(ou.UserId));
            if (NumOfUnreadMessages != 0)
            {
                this.Info22Label.Text = NumOfUnreadMessages.ToString();
                this.Info22Label.Visible = true;
                this.Info2Label.Visible = false;
            }
            else
            {
                this.DeleteAllButton.Visible = false;
                this.DeleteSelectedButton.Visible = false;
            }


            // This is a temporary place to refresh the session values.
            // Should remove this to server initiated way
            ou.RefreshOU();
        }

        protected int NewMessagesCounter(String UsrId)
        {
            string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection
            strConnection = new System.Data.SqlClient.SqlConnection(conn);
            string StrSQL = "Nivdal_GetNumberOfInternalMessages";
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UsrId));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@MessageCount", System.Data.SqlDbType.Int, 0));
            cmd.Parameters["@MessageCount"].Direction = ParameterDirection.Output;

            strConnection.Open();
            try
            {
                cmd.ExecuteNonQuery();
                return (int)(cmd.Parameters["@MessageCount"].Value);
            }

            catch (Exception SQLDBException)
            {
                Response.Write(SQLDBException.Message);
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");
                return 0;

            }

            finally
            {

                strConnection.Close();
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string AreYouSureForButtons()
        {
            string ReturnValue = "return confirm('" + Resources.Resource.AreYouSure + "')";
            return (ReturnValue);
            //return ("return confirm('Delete messages?')");
        }

        protected void DeleteAll(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            GND.ExecNivdalSQL("UserId", "Nivdal_MarkAllInstantMessagesAsRead", ou.UserId);
            Response.Redirect("InternalTeamMessages.aspx");
        }

        protected void DeleteSelected(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            foreach (GridViewRow row in GridView1.Rows)
            {
                if ((row.FindControl("DeleteCheckBox") as CheckBox).Checked)
                {
                    GND.ExecNivdalSQL("MessageId", "Nivdal_MarkInstantMessageRead", (row.FindControl("MessageIdHiddenField") as HiddenField).Value);
                }
            }
            Response.Redirect("InternalTeamMessages.aspx");
        }


    }
}